package org.peterf.foxish.util;

import java.util.Stack;

public abstract class Repository<T extends Recyclable> {

    private Stack<T> stack = new Stack<T>();

    public T get() {
        if (stack.empty()) {
            return allocate();
        } else {
            return stack.pop();
        }
    }

    public void recycle(T element) {
        element.recycle();
        stack.push(element);
    }

    protected abstract T allocate();

}
